package com.example.itces.controller;

import com.example.itces.domain.User;
import com.example.itces.service.ManagerService;
import com.example.itces.service.UserService;
import com.example.itces.utils.ResponData;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
public class ManagerController {
    @Autowired
    ManagerService managerService;
    @Autowired
    UserService userService;

    @RequestMapping("/page/admin/getAllUser")
    @ResponseBody
    public ResponData getAllUser(@RequestParam String pagenum,@RequestParam String pagesize){
        if(pagenum==null||pagenum==""||pagesize==null||pagesize==""){
            return ResponData.build(0,"获取失败");
        }else {
            int pageNum=Integer.parseInt(pagenum);
            int pageSize=Integer.parseInt(pagesize);
            PageInfo<User> list=userService.getAllUser(pageNum,pageSize);
//        System.out.println(list);
            return ResponData.ok(list);
        }
    }

    @RequestMapping("/page/admin/delUserById")
    public ResponData delUserById(@RequestParam String id){
        int Id=Integer.parseInt(id);
        Boolean b=userService.delUserById(Id);
        if(b){
            return ResponData.build(1,"删除成功");
        }else {
            return ResponData.build(0,"删除失败");
        }
    }

    @RequestMapping(value = "/page/admin/updateUserById",method = RequestMethod.POST)
    public ResponData updateUserById(User user){
        System.out.println(user);
        Boolean b=userService.updateUserById(user);
        if(b){
            return ResponData.build(1,"更新成功！");
        }else {
            return ResponData.build(0,"更新失败！");
        }
    }


    @RequestMapping(value = "/page/admin/addUser",method = RequestMethod.POST)
    public ResponData addUser(User user){
        System.out.println(user);
        Boolean b=userService.addUser(user);
        if (b){
            return ResponData.build(1,"添加成功");
        }else {
            return ResponData.build(0,"添加失败");
        }
    }
}
